package com.android.youwenchuang.ui.activity;
//商品详情
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.Nullable;

import com.android.youwenchuang.App;
import com.android.youwenchuang.base.BaseActivity;
import com.android.youwenchuang.bean.Car;
import com.android.youwenchuang.bean.Goods;
import com.android.youwenchuang.bean.Order;
import com.android.youwenchuang.databinding.ActivityDetailsBinding;
import com.android.youwenchuang.presenter.GoodsDetailsPresenter;
import com.android.youwenchuang.util.UserData;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import java.util.Date;

public class GoodsDetailsActivity extends BaseActivity {

    private ActivityDetailsBinding viewBinding;

    private GoodsDetailsPresenter presenter = new GoodsDetailsPresenter();

    private Goods goods;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityDetailsBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        initTitleBar();
        goods = GsonUtils.fromJson(getIntent().getStringExtra("data"), Goods.class);
        Glide.with(this)
                .load(goods.getPics())
                .into(viewBinding.ivImage);
        viewBinding.tvTitle.setText(goods.getName());
        viewBinding.tvPrice.setText("¥" + goods.getPrice());
        viewBinding.tvType.setText("分类：" + goods.getType());
        viewBinding.tvInfo.setText(goods.getContent());
        viewBinding.tvShop.setText("发布人：" + goods.getUser());

        viewBinding.tvBuy.setOnClickListener(view -> {
            Order order = new Order();
            order.setDate(new Date());
            order.setUserId(UserData.user.getId());
            order.setGoodsId(goods.getId());
            order.setNumber(1);
            App.mApp.getDaoSession().getOrderDao().insert(order);
            ToastUtils.showShort("购买成功");
            finish();
        });
        viewBinding.tvAdd.setOnClickListener(view -> {
            Car car = new Car();
            car.setNumber(1);
            car.setDate(new Date());
            car.setUserId(UserData.user.getId());
            car.setGoodsId(goods.getId());
            App.mApp.getDaoSession().getCarDao().insert(car);
            ToastUtils.showShort("加入购物车成功");
            finish();
        });

        viewBinding.tvDianpu.setOnClickListener(view -> {
            ToastUtils.showShort("我的店铺");
        });

        viewBinding.tvKefu.setOnClickListener(view -> {
            ToastUtils.showShort("在线客服");
        });

        viewBinding.tvPhone.setOnClickListener(view -> {
            startActivity(IntentUtils.getDialIntent("133011114577"));
        });


    }

}
